package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.Result;
import com.usian.common.ResultCodeEnum;
import com.usian.common.exption.PasswordErrorException;
import com.usian.common.exption.UsernameNotExistException;
import com.usian.mapper.UserMapper;
import com.usian.pojo.User;
import com.usian.pojo.vo.LoginReqVo;
import com.usian.service.IUserService;
import org.springframework.stereotype.Service;

@Service
public class IUserviceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public Result<User> login(LoginReqVo loginReqVo) {
        String username = loginReqVo.getUsername();
        String password = loginReqVo.getPassword();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        User user = this.baseMapper.selectOne(queryWrapper.eq("username", username));
        if (user == null){
           throw new UsernameNotExistException(ResultCodeEnum.USERNAME_NOT_EXIST);
        }else {
            if (user.getPassword().equals(password)){
                return Result.ok(user);
            }else {
                throw new PasswordErrorException(ResultCodeEnum.PASSWORD_ERROR);
            }
        }
    }

    @Override
    public Result<User> getUserById(Long id) {
        return Result.ok(this.baseMapper.selectById(id));
    }

}
